<?php
namespace App\Service;
use App\Entity\Announce;
use App\Entity\Comment;
use App\Entity\Message;
use App\Entity\ProductStatistic;
use App\Entity\ShopStatistic;
use App\Entity\Task;
use App\Entity\User;
use App\Repository\AnnounceCategoryRepository;
use App\Repository\AnnounceRepository;
use App\Repository\MessageCategoryRepository;
use App\Repository\MessageRepository;
use App\Repository\ProductRepository;
use App\Repository\UserRepository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;

class CommentService extends ServiceEntityRepository
{
    const __COMMENT_SHOP__         = 1;
    const __COMMENT_PRODUCT__      = 2;

    //支付参数设置
    public $productRepository;

    public function __construct(
        ManagerRegistry $registry
        ,ProductRepository $productRepository
    )
    {
        parent::__construct($registry, Comment::class);
        $this->productRepository = $productRepository;
    }

    public function add(User $who,$what,$content,$score=5,$type=0,$param="")
    {
        $comment = new Comment();
        $comment->setWho($who)
            ->setWhat($what)
            ->setCreateAt(new \DateTime())
            ->setContent($content)
            ->setScore($score)
            ->setType($type)
            ->setParam($param)
        ;
        $this->_em->persist($comment);$this->_em->flush();
        return $comment;
    }

    public function add2Shop(User $who,$what,$content,$score=5,$param="")
    {
        $comment = self::add($who,$what,$content,$score,self::__COMMENT_SHOP__,$param);
        //重新计算
        $qb = $this->createQueryBuilder("A");
        $qb->select(
            $qb->expr()->avg("A.score")." As score"
        );
        $qb->where("A.what = ".$what);
        $data = $qb->getQuery()->execute();
        $newScore = $data[0]["score"];
        $shop = $who->getShop();
        if ($shop) {
            if ( !$statistic = $shop->getStatistic()) {
                $statistic = new ShopStatistic();
                $statistic->setShop($shop)
                    ->setOrders(0)
                    ->setIncomes(0)
                    ->setProducts(0)
                    ->setHeart(0)
                    ->setScore(0);
            }
            $statistic->setScore($newScore);
            $this->_em->persist($statistic);
            $this->_em->flush();
        }
        return $comment;
    }

    public function add2Prod(User $who,$what,$content,$score=5,$param="")
    {
        $comment = self::add($who,$what,$content,$score,self::__COMMENT_PRODUCT__,$param);
        //重新计算
        $qb = $this->createQueryBuilder("A");
        $qb->select(
            $qb->expr()->avg("A.score")." As score"
        );
        $qb->where("A.what = ".$what);
        $data = $qb->getQuery()->execute();
        $newScore = $data[0]["score"];
        $product = $this->productRepository->find($what);
        if ($product) {
            if ( !$statistic = $product->getStatistic()) {
                $statistic = new ProductStatistic();
                $statistic->setProduct($product)
                    ->setView(0)
                    ->setHeart(0)
                    ->setScore(0);
            }
            $statistic->setScore($newScore);
            $this->_em->persist($statistic);
            $this->_em->flush();
        }
        return $comment;
    }
}